package com.jiajia.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.jiajia.server.pojo.Admin;
import com.jiajia.server.pojo.RespBean;
import com.jiajia.server.service.IAdminService;
import com.jiajia.server.utils.FastDfsUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author ZSJia
 * @date 2021/8/22 022 - 14:12
 */
@RestController
@RequestMapping("/")
public class AdminInfoController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "更新员工信息")
    @PutMapping("/admin")
    public RespBean updateAdmin(@RequestBody Admin admin , Authentication authentication){
        if (adminService.updateById(admin)){

            UsernamePasswordAuthenticationToken authenticationToken =
                    new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities());
            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "更改密码")
    @PostMapping("/")
    public RespBean updatePassWord(@RequestBody Map info){
        String oldPass = (String) info.get("oldPass");
        String Pass = (String) info.get("Pass");
        Integer adminId = (Integer) info.get("adminId");
        return adminService.updatePassWord(oldPass,Pass,adminId);

    }
    @ApiOperation(value = "更新用户头像")
    @PostMapping("/adminHeander")
    public RespBean updateUserFace ( MultipartFile file, Integer id, Authentication authentication){
        String[] upload = FastDfsUtils.upload(file);
        String url = FastDfsUtils.getTrackerUrl()+upload[0]+"/"+upload[1];
        return adminService.updateUserFace(url,id,authentication);

    }


}
